/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChartDefaultPanel.java
 *
 * Created on 08/04/2010, 22:46:38
 */

package br.ufrn.dca.scontrol.view.componentes;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.SeriesRenderingOrder;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;

/**
 *
 * @author Marco Olimpio
 */
public class ChartDefaultPanel extends javax.swing.JPanel {
// implements ActionListener
    double setPoint;
    double erro;
    double valorCol1,valCol2;
    double p,pd,pi,pid,pi_d;

    public double getErro() {
        return erro;
    }

    public void setErro(double erro) {
        this.erro = erro;
    }

    public double getP() {
        return p;
    }

    public void setP(double p) {
        this.p = p;
    }

    public double getPd() {
        return pd;
    }

    public void setPd(double pd) {
        this.pd = pd;
    }

    public double getPi() {
        return pi;
    }

    public void setPi(double pi) {
        this.pi = pi;
    }

    public double getPi_d() {
        return pi_d;
    }

    public void setPi_d(double pi_d) {
        this.pi_d = pi_d;
    }

    public double getPid() {
        return pid;
    }

    public void setPid(double pid) {
        this.pid = pid;
    }

    public double getSetPoint() {
        return setPoint;
    }

    public void setSetPoint(double setPoint) {
        this.setPoint = setPoint;
    }

    public double getValCol2() {
        return valCol2;
    }

    public void setValCol2(double valCol2) {
        this.valCol2 = valCol2;
    }

    public double getValorCol1() {
        return valorCol1;
    }

    public void setValorCol1(double valorCol1) {
        this.valorCol1 = valorCol1;
    }


    /** The number of subplots. */
    public static final int SUBPLOT_COUNT = 2;

    /** The datasets. */
    private TimeSeriesCollection[] datasets;

    /** The most recent value added to series 1. */
    private double[] lastValue = new double[SUBPLOT_COUNT];

    public ChartPanel frame;
    /** Creates new form ChartDefaultPanel */
    public ChartDefaultPanel() {
        initComponents();
        //General chart, with 2 subplots
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis("Time"));
        this.datasets = new TimeSeriesCollection[SUBPLOT_COUNT];

//        for (int i = 0; i < SUBPLOT_COUNT; i++) {
//            this.lastValue[i] = 100.0;
//            final TimeSeries series = new TimeSeries("Random " + i, Millisecond.class);
//            this.datasets[i] = new TimeSeriesCollection(series);
//            final NumberAxis rangeAxis = new NumberAxis("Y" + i);
//            rangeAxis.setAutoRangeIncludesZero(false);
//            final XYPlot subplot = new XYPlot(
//                    this.datasets[i], null, rangeAxis, new StandardXYItemRenderer()
//            );
//            subplot.setBackgroundPaint(Color.lightGray);
//            subplot.setDomainGridlinePaint(Color.white);
//            subplot.setRangeGridlinePaint(Color.white);
//            plot.add(subplot);
//        }

        this.lastValue[0] = 100.0;

        //serie do tanque 1
        final TimeSeries seriesT1 = new TimeSeries("Tensão T1", Millisecond.class);
        this.datasets[0] = new TimeSeriesCollection(seriesT1);

        //serie do tanque 2
        final TimeSeries seriesT2 = new TimeSeries("Tensão T2", Millisecond.class);
        this.datasets[0].addSeries(seriesT2);

        //serie de SetPoint
        final TimeSeries seriesSPoint = new TimeSeries("SetPoint", Millisecond.class);
        this.datasets[0].addSeries(seriesSPoint);

        //serie de erro
        final TimeSeries seriesError = new TimeSeries("Erro de Regime", Millisecond.class);
        this.datasets[0].addSeries(seriesError);

        //serie de acomodacao superior
        final TimeSeries seriesRangeTop = new TimeSeries("", Millisecond.class);
        this.datasets[0].addSeries(seriesRangeTop);

        //serie de acomodacao inferior
        final TimeSeries seriesRangeDown = new TimeSeries("", Millisecond.class);
        this.datasets[0].addSeries(seriesRangeDown);

        final NumberAxis rangeAxis = new NumberAxis("Tensão");

        rangeAxis.setAutoRangeIncludesZero(false);
        
        final XYPlot subplotTanques = new XYPlot(
                this.datasets[0], null, rangeAxis, new StandardXYItemRenderer()
        );

        subplotTanques.setBackgroundPaint(Color.darkGray);
        subplotTanques.setDomainGridlinePaint(Color.white);
        subplotTanques.setRangeGridlinePaint(Color.white);

        plot.add(subplotTanques);


        this.lastValue[1] = 100.0;

        //dados do controlador P
        final TimeSeries seriesP = new TimeSeries("P", Millisecond.class);
        this.datasets[1] = new TimeSeriesCollection(seriesP);

        //dados do controlador PI
        final TimeSeries seriesPI = new TimeSeries("PI", Millisecond.class);
        this.datasets[1].addSeries(seriesPI);

        //dados do controlador PD
        final TimeSeries seriesPD = new TimeSeries("PD", Millisecond.class);
        this.datasets[1].addSeries(seriesPD);

        //dados do controlador PID
        final TimeSeries seriesPID = new TimeSeries("PID", Millisecond.class);
        this.datasets[1].addSeries(seriesPID);

        //dados do controlador PID
        final TimeSeries seriesPI_D = new TimeSeries("PI-D", Millisecond.class);
        this.datasets[1].addSeries(seriesPI_D);



        final NumberAxis rangeAxisParametros = new NumberAxis("Parâmetros");
        rangeAxisParametros.setAutoRangeIncludesZero(true);
        final XYPlot subplotParametros = new XYPlot(
                this.datasets[1], null, rangeAxisParametros, new StandardXYItemRenderer()
        );
        subplotParametros.setBackgroundPaint(Color.darkGray);
        subplotParametros.setDomainGridlinePaint(Color.white);
        subplotParametros.setRangeGridlinePaint(Color.white);
        plot.add(subplotParametros);


        final JFreeChart chart = new JFreeChart("", plot);
        chart.setBorderPaint(Color.black);
        chart.setBorderVisible(true);
        chart.setBackgroundPaint(Color.white);

        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        //plot.set

        final ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(60000.0);  // 60 seconds

        frame = new ChartPanel(chart);
        this.jPanel2.add(frame);
        frame.setSize(this.getSize());
        frame.setVisible(true);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox5 = new javax.swing.JCheckBox();
        jCheckBox6 = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jCheckBox1.setText("Tensão T1");

        jCheckBox2.setText("Tensão T2");

        jCheckBox3.setText("jCheckBox1");

        jCheckBox4.setText("jCheckBox1");

        jCheckBox5.setText("jCheckBox1");

        jCheckBox6.setText("jCheckBox1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox2)
                    .addComponent(jCheckBox3)
                    .addComponent(jCheckBox4)
                    .addComponent(jCheckBox5)
                    .addComponent(jCheckBox6))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox6)
                .addContainerGap(149, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 282, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 303, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        frame.setSize(this.jPanel2.getSize());

    }//GEN-LAST:event_formComponentResized

//    @Override
//    public void actionPerformed(ActionEvent e) {
////        if(e.getActionCommand() == "atualiza"){
////
////        }
//        //System.out.println("CHAMOUS!");
//        this.datasets[0].getSeries(0).add(new Millisecond(),getValorCol1());
//        this.datasets[0].getSeries(1).add(new Millisecond(),getValCol2());
//        this.datasets[0].getSeries(2).add(new Millisecond(),getSetPoint());
//        this.datasets[0].getSeries(3).add(new Millisecond(),getErro());
//
//        this.datasets[1].getSeries(0).add(new Millisecond(),getP());
//        this.datasets[1].getSeries(1).add(new Millisecond(),getPi());
//        this.datasets[1].getSeries(2).add(new Millisecond(),getPd());
//        this.datasets[1].getSeries(3).add(new Millisecond(),getPid());
//        this.datasets[1].getSeries(4).add(new Millisecond(),getPi_d());
//    }

    public void add() {
//        if(e.getActionCommand() == "atualiza"){
//
//        }
        //System.out.println("CHAMOUS!");
        this.datasets[0].getSeries(0).add(new Millisecond(),getValorCol1());
        this.datasets[0].getSeries(1).add(new Millisecond(),getValCol2());
        this.datasets[0].getSeries(2).add(new Millisecond(),getSetPoint());
        this.datasets[0].getSeries(3).add(new Millisecond(),getErro());
        this.datasets[0].getSeries(4).add(new Millisecond(),(getSetPoint()*(double)1.02));
        this.datasets[0].getSeries(5).add(new Millisecond(),(getSetPoint()*(double)0.98));

        this.datasets[1].getSeries(0).add(new Millisecond(),getP());
        this.datasets[1].getSeries(1).add(new Millisecond(),getPi());
        this.datasets[1].getSeries(2).add(new Millisecond(),getPd());
        this.datasets[1].getSeries(3).add(new Millisecond(),getPid());
        this.datasets[1].getSeries(4).add(new Millisecond(),getPi_d());
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JCheckBox jCheckBox6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

}
